﻿using Star_Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

/// <summary>
/// DoDom 的摘要说明
/// </summary>
public class DoDom
{
    public DoDom()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }

    /// <summary>
    /// 生成div,元素名,内容,class名,跳过前面参数的话用 "" ;
    /// </summary>
    /// <param name="s1">元素名</param>
    /// <param name="s2">元素内容</param>
    /// <param name="sx">类名</param>
    /// <returns></returns>

    public string credDom(string s1, params string[] s2)
    {
        StringBuilder s = new StringBuilder();
        string sx1 = "";
        string sx2 = "";
        if (s2.Length == 1)
        {
            sx1 = s2[0];
        }
        if (s2.Length == 2)
        {
            sx1 = s2[0];
            sx2 = s2[1];
        }
        s.AppendFormat("<{0} class='{2}'>{1}</{0}>", s1, sx1, sx2);

        return s.ToString();
    }

    public string creadDiv(params string[] s2)
    {
        StringBuilder s = new StringBuilder();
        string sx1 = "";
        string sx2 = "";
        if (s2.Length == 1)
        {
            sx1 = s2[0];
        }
        if (s2.Length == 2)
        {
            sx1 = s2[0];
            sx2 = s2[1];
        }
        s.AppendFormat("<div class='{2}'>{1}</{div>", sx1, sx2);

        return s.ToString();
    }

    /// <summary>
    /// 类别下的商品表 插入在<div id="ftable">下,cnt参数,定义表的列数
    /// </summary>
    /// <param name="s1"></param>
    /// <returns></returns>
    public string creadTable(string sx2)
    {
        domLog(sx2);
        List<string> s1 = new DoDB().takeSp(sx2);
        int cnt = 6;
        StringBuilder sx = new StringBuilder();
        StringBuilder s = new StringBuilder();
        int k = s1.Count % cnt;
        int len = (cnt - k);

        for (int j = 0; j < len; j++)
        {
            s1.Add("");
        }
        for (int i = 1; i <= s1.Count; i++)
        {
            s.Append(credDom("td", s1[i - 1]));
            if (i % cnt == 0)
            {
                sx.Append(credDom("tr", s.ToString()));
                s = new StringBuilder();
            }
        }
        return new StringBuilder().AppendFormat("<table border='1'>{0}</table>", sx).ToString();

    }

    /// <summary>
    /// 类别块 插入在<div class="fleibie">下
    /// </summary>
    /// <param name="s1"></param>
    /// <returns></returns>
    public string creaLeiBie()
    {
        List<string> s1 = new DoDB().takeLeiBie();
        StringBuilder sx = new StringBuilder();
        for (int i = 0; i < s1.Count; i++)
        {
            sx.AppendFormat(@"<div class='leibie'>{0}</div>", s1[i]);
        }
        return sx.ToString();
    }

    /// <summary>
    /// 操作选项子元素 插<select class='selelei'>下
    /// </summary>
    /// <returns></returns>
    public string creaOp()
    {
        return new dataFunc().getSelectCheckFromDb(1, "TSshangpin", "leibie","leibie"," id >0");
    }

    /// <summary>
    /// 操作选项子元素 插 <select class='seleshang'>下
    /// </summary>
    /// <param name="sx"></param>
    /// <returns></returns>
    public string creaOp2(string sx)
    {
        return new dataFunc().getSelectCheckFromDb(1, "TSshangpin", "shangpin", "shangpin",string.Format(" id >0 AND leibie='{0}'",sx) );
    }
    /// <summary>
    /// 库操作记录表 插在 <div class='caozuo'>下
    /// </summary>
    /// <param name="s1"></param>
    /// <returns></returns>
    public string creaJiTable(int rc)//库操作记录表  <div class='caozuo'>下
    {
        List<object[]> s1;
        string s3 ="";
        if (rc == 1)//入库
        {
            s1 = new DoDB().takeJiLuR();
            s3 = "成本";
        }
        else//出库
        {
            s1 = new DoDB().takeJiLuC();
            s3 = "单价";
        }
        StringBuilder sx = new StringBuilder();
        StringBuilder s = new StringBuilder();
        for (int i = 0; i < s1.Count; i++)//商品 , 成本价 , 入库数量 , 总计 , 操作员 , 时间 , 
        {
            s.AppendFormat(@"<tr>
							<td>{0}</td>
							<td>{1}</td>
							<td>{2}</td>
							<td>{3}</td>
							<td>{4}</td>
							<td>{5}</td>
                            </tr>", s1[i][0], s1[i][1], s1[i][2], s1[i][3], s1[i][4], s1[i][5]);
        }
        sx.AppendFormat(@"<table border='1'><tr><th>商品</th><th>{1}</th><th>数量</th><th>总计</th><th>操作员</th><th>时间</th></tr>{0} </table>", s,s3);
        return sx.ToString();
    }

    /// <summary>
    /// 商品详情 插<div class='fe2tag'>下
    /// </summary>
    /// <param name="list"></param>
    /// <returns></returns>
    public string creaFe2tag(string s1)
    {
        domLog("creaFe2tag"+s1);
        List <string> list = new DoDB().takeShangPin(s1);
        StringBuilder sx = new StringBuilder();
        sx.AppendFormat(@"
							<div>
								商品详情
							</div>
							<div>
								<div>
									{0}
								</div>
								<div>
								</div>
							</div>
							<div>
								<div>
									<div>
										单价
										<div>
											{1}
										</div>
									</div>
									<div>
										成本
										<div>
											{2}
										</div>
									</div>
									<br />
								</div>
								<div>
									<div>
										入库
										<div>
											{3}
										</div>
									</div>
									<div>
										售出
										<div>
											{4}
										</div>
									</div>
								</div>
								<div>
									<div>
										盈利
										<div>
											{5}
										</div>
									</div>
									<div>
										库存
										<div>
											{6}
										</div>
									</div>
								</div>
							</div>
						", list[0], list[1], list[2], list[3], list[4], list[5], list[6]);
        return sx.ToString();
    }
    public string creaW()//主框架,带商品明细
    {
        DoDB dodb = new DoDB();
        object[] list = dodb.takeZongE();
        StringBuilder sx = new StringBuilder();
        sx.AppendFormat(@" <div class='w'>
			<div class='head'>
				head
			</div>
			<div class='layer'>
				<div class='sidebar'>
					<div class='s1'>
						<div>账号</div>
						<div>{0}</div>
						<div>权限:管理员</div>
					</div>
					<div class='s2'>
						<div class='x1'>商品明细</div>
						<div class='x2'>商品存入</div>
						<div class='x3'>商品支出</div>
					</div>
				</div>
				<div class='main'>
					{1}
				</div>
			</div>

		</div>", dodb.takeDangqianYH(), creaM1a());
        //参数: 用户 ,4个list是总额,
        return sx.ToString();
    }

    public string creaM1a()//商品明细
    {
        DoDB dodb = new DoDB();
        object[] list = dodb.takeZongE();
        StringBuilder sx = new StringBuilder();
        sx.AppendFormat(@"<div class='m1'>
						商品明细
					</div>
					<div class='m2'>
						<div class='fet1zong'>
							<div>
								本月第 {0} 天,当前总额:
							</div>
							<div>
								<div>
									<div>
										<p>支出</p>
										<p>{1}</p>
									</div>
									<div></div>
								</div>
								<div>
									<div>
										<p>收入</p>
										<p>{2}</p>
									</div>
									<div></div>
								</div>
								<div>
									<div>
										<p>盈利</p>
										<p>{3}</p>
									</div>
									<div></div>
								</div>
								<div>
									<div>
										<p>库存</p>
										<p>{4}</p>
									</div>
									<div class='deal-rect'></div>
								</div>
							</div>
						</div>
					</div>
					<div class='m3'>
						<div class='fe1table'>
							<div>
								商品列表
							</div>
							<div class='fleibie'>
								{5}
							</div>
							<div class='ftable'>
								{6}
							</div>

						</div>
						<div class='fe2tag'>
							{7}
						</div>
					</div>
					<div class='m4'>
						<div class='fet--'>
							商品细节
						</div>
					</div>", "10", list[0], list[1], list[2], list[3], creaLeiBie(), creadTable("类别0"), creaFe2tag("商品01"));
        return sx.ToString();
    }

    public string creaM1b()//入库
    {
        StringBuilder sx = new StringBuilder();
        sx.AppendFormat(@"<div class='m1'>
						商品入库
					</div>

					<div class='mb2'>
						<div class='mb21'>

							<div class='fet2ku'>
								<div class=''>
									商品入库
								</div>

								<div>
									<div>
										<span>....</span>
									</div>
									<div>
										<span>选择类别</span>
										<select class='selelei'>
											{1}
										</select>
										<span>选择产品</span>
										<select class='seleshang'>
											{2}
										</select>
										<span>入库数量</span>
										<input type='text' class='inp1' />
										<input type='button'class='ruku' value='确认' />
									</div>
								</div>
							</div>
							<div>

							</div>
							<div class='fe3table'>
								<div>
									入库记录
								</div>

								<div class='caozuo'>
									{0}
								</div>

							</div>
						</div>
					</div>", creaJiTable(1),creaOp(),creaOp2("类别0"));

        return sx.ToString();
    }

    public string creaM1c()
    {
        StringBuilder sx = new StringBuilder();
        sx.AppendFormat(@"<div class='m1'>
						商品出库
					</div>

					<div class='mb2'>
						<div class='mb21'>

							<div class='fet2ku'>
								<div class=''>
									商品出库
								</div>

								<div>
									<div>
										<span>....</span>
									</div>
									<div>
										<span>选择类别</span>
										<select class='selelei'>
											{1}
										</select>
										<span>选择产品</span>
										<select class='seleshang'>
											{2}
										</select>
										<span>出库数量</span>
										<input type='text' class='inp1'  />
										<input type='button'class='chuku' value='确认' />
									</div>
								</div>
							</div>
							<div>

							</div>
							<div class='fe3table'>
								<div>
									出库记录
								</div>

								<div class='caozuo'>
									{0}
								</div>

							</div>
						</div>
					</div>", creaJiTable(2), creaOp(), creaOp2("类别0"));

        return sx.ToString();
    }
    public void domLog(string sx)
    {
        dataFunc.writeDoLogs("dom请求详情", sx, "domLog", 1);
    }
}